package de.tu_darmstadt.gdi.battleship;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

/**
 * Provides sound functionality
 * 
 * @author Mischa Lundberg
 * @version 1.5
 */
public class Sound {

	private boolean muted;
	
	/**
	 * Creates a new Sound instance
	 */
	Sound() {
		muted = false;
	}

	/**
	 * Plays an aplause sound when the level is solved.
	 */
	public void playSolvedSound() {
		 if(muted) return;
	     try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/sound/Applaus.wav"));
            AudioFormat af = audioInputStream.getFormat();
            int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio  = new byte[size];
            DataLine.Info info  = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);
            
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
          
	        }catch(Exception e) { 
	        	e.printStackTrace(); 
	        }
	    }

	/**
	 * Plays a sound when two bricks are removed from the game area.
	 */
	public void playShotInWaterSound() {
		if(muted) return;
		try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/sound/Boing.wav"));
            AudioFormat af = audioInputStream.getFormat();
            int size  = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio = new byte[size];
            DataLine.Info info = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
           
        }catch(Exception e) { 
        	e.printStackTrace(); 
        } 
	}
	
	/**
	 * Plays a sound when no more moves are possible.
	 * Currently unused.
	 */
	public void playShotOnShipSound() {
		if(muted) return;
		try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/sound/schrei.wav"));
            AudioFormat af = audioInputStream.getFormat();
            int size  = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio = new byte[size];
            DataLine.Info info = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size); 
          
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
           
        }catch(Exception e) { 
        	e.printStackTrace(); 
        } 
	}
	     
	/**
	 * Plays a sound for the highscore.
	 * Currently unused.
	 */
	public void playHighscoreSound() {
		if(muted) return;
		try{
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/sound/Highscore.wav"));
            AudioFormat af = audioInputStream.getFormat();
            int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio = new byte[size];
            DataLine.Info info = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);
         
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
           
        }catch(Exception e){ 
        	e.printStackTrace(); 
        }
	}
	       
	/**
	 * Toggles sound output
	 * 
	 * @param b
	 */
	public void setEnabled(boolean b) {
		muted = b;
	}
	
	/**
	 * @return wether the sound is on or off
	 */
	public boolean getEnabled() {
		return muted;
	}
}